package cm.cool.web.service.coin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;

import cm.cool.web.bean.CoinDto;
import cm.cool.web.data.CoinMapper;
import cm.cool.web.util.respon.PaginationDto;
import cm.cool.web.util.respon.R;
import cm.cool.web.util.respon.ResultEncoding;

@Service
public class CoinService {
	@Autowired
	CoinMapper coinMapper;

	/**
	 * 条件查找所有货币记录
	 * 
	 * @param record
	 * @return
	 */
	public R findUserNamecoinDtoCount(CoinDto record, PaginationDto paginationDto) {

		R r = new R();
		try {
			PageHelper.startPage(paginationDto.getCurrentPageNum(), paginationDto.getPerPageSize());
			paginationDto.setEntitys(coinMapper.findUserNamecoinDtoCount(record));
			int count = coinMapper.findcoinCount(record);
			paginationDto.setTotalCount(count);
			paginationDto.setTotalPageNum((int) Math.ceil((count * 1.0) / paginationDto.getPerPageSize()));
			r.setData(paginationDto);
		} catch (Exception e) {
			e.printStackTrace();
			r.setCode(ResultEncoding.R_ERR);
			r.setMsg("查询异常");
			return r;
		}
		return r;
	}

	/**
	 * 根据ID删除一条记录
	 * 
	 * @param id
	 * @return
	 */
	public R deleteByPrimaryKey(String id) {
		R r = new R();
		String str = String.valueOf(id);
		// 数据校验
		if (R.isNull(str)) {
			r.setCode(ResultEncoding.R_PARAMETER);
			r.setMsg("参数为空,删除失败");
			return r;
		}

		if (coinMapper.deleteByPrimaryKey(id) == 1) {
			r.setMsg("删除成功");
		} else {
			r.setCode(ResultEncoding.R_ERR);
			r.setMsg("删除异常");
		}

		return r;
	}

	/**
	 * 修改货币
	 * 
	 * @param coin
	 * 
	 */

	public R updateByPrimaryKeySelective(CoinDto record) {
		R r = new R();
		try {
			if (R.isNull(record.getCoinNo() + "") || R.isNull(record.getCoinName()) || R.isNull(record.getHost())
					|| R.isNull(record.getPort()) || record.getIcon() == null || R.isNull(record.getWalletName())
					|| R.isNull(record.getWalletLockpwd()) || R.isNull(String.valueOf(record.getChangeFee()))
					|| R.isNull(record.getWalletPwd())) {

				r.setCode(ResultEncoding.R_PARAMETER);
				r.setMsg("参数缺失,修改失败");
				return r;

			}

			if (coinMapper.updateByPrimaryKeySelective(record) == 1) {
				r.setMsg("修改成功");
				return r;
			} else {
				r.setCode(ResultEncoding.R_ERR);
				r.setMsg("修改异常");
				return r;
			}

		} catch (Exception e) {
			e.printStackTrace();
			r.setCode(ResultEncoding.R_ERR);
			r.setMsg("系统异常");
			return r;
		}

	}

	/**
	 * 添加货币
	 * 
	 * @param coin
	 * 
	 */
	public R insert(CoinDto coin) {

		R r = new R();

		if (R.isNull(coin.getCoinNo() + "") || R.isNull(coin.getCoinName()) || R.isNull(coin.getHost())
				|| R.isNull(coin.getPort()) || coin.getIcon() == null || R.isNull(coin.getWalletName())
				|| R.isNull(coin.getWalletLockpwd()) || R.isNull(String.valueOf(coin.getChangeFee()))
				|| R.isNull(coin.getWalletPwd())) {
			r.setMsg("参数缺失,添加失败");
			r.setCode(ResultEncoding.R_PARAMETER);
			return r;
		}
		
		

		try {
			// 判断数据操作是否正确

			if (coinMapper.insert(coin) == 1) {
				r.setMsg("添加成功");
				return r;
			} else {
				r.setCode(ResultEncoding.R_ERR);
				r.setMsg("添加异常");
				return r;
			}

		} catch (Exception e) {
			e.printStackTrace();
			r.setMsg("添加失败");
			r.setCode(ResultEncoding.R_ERR);
			return r;
		}

	}
}
